VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Address"
Attribute VB_Creatable = False
Attribute VB_Exposed = True
Option Explicit

' the following pertain to being supported by
'   VBOFCollection, VBOFObjectManager and
'   VBOFEventManager
Public ObjectID As Long
Public ObjectChanged As Long
Public ObjectAdded As Long
Public ObjectDeleted As Long
Public ObjectParentCount As Long
Public ObjectManager As VBOFObjectManager

' the following code pertains to the business
'   of the Address object
Private pvtLine1 As String
Private pvtLine2 As String
Private pvtLine3 As String
Private pvtCity As String
Private pvtStateCode As String
Private pvtState As State
Private pvtStateObjectID As Long
Private pvtZipCode As Long
Private pvtZipSupplement As Integer
Private pvtZipExtension As Integer
Private pvtForeignZipCode As String
Private pvtStatus As String
Private pvtUsage As String
Public StateCodes As VBOFCollection

Public Function ObjectDBGridUnboundAddData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional NewRowBookmark As Variant) As Boolean
' Populate the object variables with the values
'   provided by the user in the new row of the
'   DBGrid
'   (in support of VBOFCollection)
'
' Parameter Description:
'   DBGrid:= the DBGrid which is being
'       populated
'   RowBuf:= the current DBGrid RowBuf object
'   NewRowBookmark:= the row number being processed

    Dim I As Long
    
    For I = 0 To RowBuf.ColumnCount - 1
        If Not IsNull(RowBuf.Value(0, I)) Then
            Select Case RowBuf.ColumnName(I)
                Case "Line1"
                    Line1 = RowBuf.Value(0, I) & ""
                Case "Line2"
                    Line2 = RowBuf.Value(0, I) & ""
                Case "Line3"
                    Line3 = RowBuf.Value(0, I) & ""
                Case "City"
                    City = RowBuf.Value(0, I) & ""
                Case "StateCode"
                    StateCode = RowBuf.Value(0, I) & ""
                Case "ZipCode"
                    ZipCode = CLng("0" & RowBuf.Value(0, I))
                Case "ZipSupplement"
                    ZipSupplement = CLng("0" & RowBuf.Value(0, I))
                Case "ZipExtension"
                    ZipExtension = CLng("0" & RowBuf.Value(0, I))
                Case "Status"
                    Status = RowBuf.Value(0, I) & ""
                Case "Usage"
                    Usage = RowBuf.Value(0, I) & ""
    
' Note:  Do not initialize the ObjectID.
        
            End Select
        End If
    Next I
    
' return "OK" status
    ObjectDBGridUnboundAddData = True
End Function

Public Function ObjectDBGridUnboundReadData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional RowNumber As Variant) As Boolean
' Populate the DBGrid RowBuf with values from
'   variables within this object
'   (in support of VBOFCollection)
'
' Parameter Description:
'   DBGrid:= the DBGrid which is being
'       populated
'   RowBuf:= the current DBGrid RowBuf object
'   RowNumber:= the row number being processed

    Dim I As Long
    
    For I = 0 To RowBuf.ColumnCount - 1
        Select Case RowBuf.ColumnName(I)
            Case "Line1"
                RowBuf.Value(RowNumber, I) = Line1
            Case "Line2"
                RowBuf.Value(RowNumber, I) = Line2
            Case "Line3"
                RowBuf.Value(RowNumber, I) = Line3
            Case "City"
                RowBuf.Value(RowNumber, I) = City
            Case "StateCode"
                RowBuf.Value(RowNumber, I) = StateCode
            Case "ZipCode"
                RowBuf.Value(RowNumber, I) = ZipCode
            Case "ZipSupplement"
                RowBuf.Value(RowNumber, I) = ZipSupplement
            Case "ZipExtension"
                RowBuf.Value(RowNumber, I) = ZipExtension
            Case "Status"
                RowBuf.Value(RowNumber, I) = Status
            Case "Usage"
                RowBuf.Value(RowNumber, I) = Usage
            Case "FormattedZip"
                RowBuf.Value(RowNumber, I) = FormattedZip
            Case "ForeignZipCode"
                RowBuf.Value(RowNumber, I) = ForeignZipCode
            Case "ObjectID"
                RowBuf.Value(RowNumber, I) = ObjectID
        End Select
    Next I
End Function
Public Function ObjectDataSource() As String
' Return the Data Source with which this Class is associated
'   (in support of VBOFCollection)
    
    ObjectDataSource = "Addresses"
End Function

Public Function FormattedAddress() As String
' Return a displayable, fully formatted
'   version of Me

    Dim ReturnString As String
    
    If Line1 > "" Then
        ReturnString = _
            pvtContatenateWithCRLF( _
                String1:=ReturnString, _
                String2:=Line1)
    End If
    
    If Line2 > "" Then
        ReturnString = _
            pvtContatenateWithCRLF( _
                String1:=ReturnString, _
                String2:=Line2)
    End If
    
    If Line3 > "" Then
        ReturnString = _
            pvtContatenateWithCRLF( _
                String1:=ReturnString, _
                String2:=Line3)
    End If
    
    If City > "" Then
        ReturnString = _
            pvtContatenateWithCRLF( _
                String1:=ReturnString, _
                String2:=City) _
                & ", "
    End If
    
' the conventional (i.e. GUI-centric) VB programming
'   techniques must use StateCode.  The Most
'   Totally Coolest way (the OO way) is to imbed the
'   entire State object within the Address.  Yields
'   not only the StateCode, but anything else known
'   about that State
    If Not pvtState Is Nothing Then
        ReturnString = ReturnString & pvtState.StateCode
    ElseIf StateCode > "" Then
        ReturnString = ReturnString & pvtStateCode
    End If
    
    If ZipCode >= 0 Then
        ReturnString = ReturnString & " " & FormattedZip
    End If
    
'' show-off a little by revealing interesting information
''   available only through having a rich object
''   model
'    If Not pvtState Is Nothing Then
'        ReturnString = _
'            pvtContatenateWithCRLF( _
'                String1:=ReturnString, _
'                String2:="(State's Capital is " & _
'                    pvtState.CapitalCity & _
'                ")")
'    End If
    
    FormattedAddress = ReturnString
End Function

Public Function ObjectSortCompare(Optional SortField As Variant, Optional SortOrder As Variant, Optional CompareObject As Variant) As Long
' Support the Collection.Sort method
' Note: use the ObjectManager.ObjectSortCompare
'   method for assistance

    Select Case SortField
        Case "FormattedAddress"
            ObjectSortCompare = _
                ObjectManager.ObjectSortCompare( _
                    Value1:=FormattedAddress, _
                    Value2:=CompareObject.FormattedAddress, _
                    SortOrder:=SortOrder)
        
        Case "City"
            ObjectSortCompare = _
                ObjectManager.ObjectSortCompare( _
                    Value1:=City, _
                    Value2:=CompareObject.City, _
                    SortOrder:=SortOrder)
        
        Case "StateCode"
            ObjectSortCompare = _
                ObjectManager.ObjectSortCompare( _
                    Value1:=State.StateCode, _
                    Value2:=CompareObject.State.StateCode, _
                    SortOrder:=SortOrder)
        
        Case "StateCapitalCity"
            ObjectSortCompare = _
                ObjectManager.ObjectSortCompare( _
                    Value1:=State.CapitalCity, _
                    Value2:=CompareObject.State.CapitalCity, _
                    SortOrder:=SortOrder)
        
        Case "ZipCode"
            ObjectSortCompare = _
                ObjectManager.ObjectSortCompare( _
                    Value1:=FormattedZip, _
                    Value2:=CompareObject.FormattedZip, _
                    SortOrder:=SortOrder)
    End Select

End Function

Private Function pvtContatenateWithCRLF(Optional String1 As Variant, Optional String2 As Variant) As String

    Dim ReturnString As String
    
    ReturnString = ""
    
    If Not IsMissing(String1) Then
        ReturnString = String1
        If String1 > "" Then
            ReturnString = ReturnString & vbCrLf
        End If
    End If
    
    If Not IsMissing(String2) Then
        If String2 > "" Then
            ReturnString = ReturnString & String2
        End If
    End If

    pvtContatenateWithCRLF = ReturnString
End Function
Public Function FormattedZip() As String

    Dim ReturnString As String
    
    ReturnString = ""
    
    If ZipCode >= 0 Then
        ReturnString = ReturnString & Format$(ZipCode, "00000")
    End If
    
    If ZipSupplement > 0 Then
        If ReturnString = "" Then
            ReturnString = Format$(ZipSupplement, "0000")
        Else
            ReturnString = ReturnString & "-" & Format$(ZipSupplement, "0000")
        End If
    End If
    
    If ZipExtension > 0 Then
        If ReturnString = "" Then
            ReturnString = Format$(ZipExtension, "00")
        Else
            ReturnString = ReturnString & "-" & Format$(ZipExtension, "00")
        End If
    End If

    FormattedZip = ReturnString
End Function


Public Function SetStateFromStateCode(StateCode As String) As Variant
' Convert the StateCode value to a State object

    Dim NewState As New State
    
    Set NewState = _
        ObjectManager. _
            NewObject( _
                Sample:=NewState, _
                WhereClause:="StateCode = '" & StateCode & "'")
                
    If Not NewState Is Nothing Then
        Set State = NewState
        Set SetStateFromStateCode = NewState
    Else
        Set SetStateFromStateCode = Nothing
    End If
    
    Set NewState = Nothing
End Function

Public Property Get State() As State
    Set State = pvtState
End Property


Public Property Set State(aState As State)
    Set pvtState = aState
End Property

Private Sub Class_Initialize()
   
    Set ObjectManager = Nothing
    
    ZipCode = -1
    ZipSupplement = -1
    ZipExtension = -1
    ForeignZipCode = ""
    Status = "Current"
    Usage = "Primary"

End Sub




Public Function ObjectInitializeFromRecordSet(Optional RecordSet As Variant) As Address
' Populate my variables from the RecordSet
'   (in support of VBOFCollection)

    Dim NewState As New State

    On Local Error Resume Next
    
    Line1 = RecordSet("Line1")
    Line2 = RecordSet("Line2")
    Line3 = RecordSet("Line3")
    City = RecordSet("City") & ""
    ZipCode = RecordSet("ZipCode") + 0
    ZipSupplement = RecordSet("ZipSupplement") + 0
    ZipExtension = RecordSet("ZipExtension") + 0
    ForeignZipCode = RecordSet("ForeignZipCode") & ""
    Status = RecordSet("Status") & ""
    Usage = RecordSet("Usage") & ""
    
    ObjectID = RecordSet("ObjectID")
    
' pick-up the State object
    If Not IsNull(RecordSet("StateObjectID")) Then
        Set pvtState = _
            ObjectManager. _
                NewObject( _
                    Sample:=NewState, _
                    ObjectID:=CStr(RecordSet("StateObjectID")))
    End If

    Set ObjectInitializeFromRecordSet = Me
End Function

Public Function ObjectListBoxValue() As String
' Return a String will represent this object
'   in a ListBox
'   (in support of VBOFCollection)

    ObjectListBoxValue = _
        Line1 & " " & _
        City & ", " & _
        StateCode & " " & _
        FormattedZip

End Function


Public Function ObjectNewInstanceOfMyClass() As Address
' Return a new instance of this class
'   (in support of VBOFCollection)

    Set ObjectNewInstanceOfMyClass = New Address
End Function



Public Function ObjectInitializeRecordSet(Optional RecordSet As Variant) As Long
' Populate the RecordSet with my variables.
' Note:  Do not initialize the ObjectID column.
' Return any error code encountered.
'   (in support of VBOFCollection)

    On Local Error GoTo InitializeRecordSet_SetError
    Err = 0
    
    RecordSet("Line1") = Line1
    RecordSet("Line2") = Line2
    RecordSet("Line3") = Line3
    RecordSet("City") = City
    RecordSet("ZipCode") = ZipCode
    RecordSet("ZipSupplement") = ZipSupplement
    RecordSet("ZipExtension") = ZipExtension
    RecordSet("ForeignZipCode") = ForeignZipCode
    RecordSet("Status") = Status
    RecordSet("Usage") = Usage
    
' set the State object
    If Not pvtState Is Nothing Then
        RecordSet("StateObjectID") = _
            pvtState.ObjectID
    Else
        RecordSet("StateObjectID") = Null
    End If
    
' Note:  Do not initialize the ObjectID column.
    
    GoTo InitializeRecordSet_SetError

InitializeRecordSet_SetError:
    ObjectInitializeRecordSet = Err
    Exit Function
End Function

Private Sub Class_Terminate()
    If Not ObjectManager Is Nothing Then
        ObjectManager.TerminateObject _
            Object:=Me
    End If
End Sub

Public Property Get Line1() As String
    Line1 = pvtLine1
End Property

Public Property Let Line1(aString As String)
    pvtLine1 = aString
'    ObjectHasChanged
End Property

Public Property Get Line2() As String
    Line2 = pvtLine2
End Property

Public Property Let Line2(aString As String)
    pvtLine2 = aString
'    ObjectHasChanged
End Property

Public Property Get Line3() As String
    Line3 = pvtLine3
End Property

Public Property Let Line3(aString As String)
    pvtLine3 = aString
'    ObjectHasChanged
End Property

Public Property Get City() As String
    City = pvtCity
End Property

Public Property Let City(aString As String)
    pvtCity = aString
'    ObjectHasChanged
End Property

Public Property Get StateCode() As String
    
    If Not pvtState Is Nothing Then
        StateCode = pvtState.StateCode
    Else
        StateCode = pvtStateCode
    End If
End Property


Public Property Let StateCode(aString As String)
' StateCode is not really stored in the Address
'   object (that would be too conventional.)  To
'   expoit the power of OO, the Address object
'   stores a reference to the entire State object,
'   not just its StateCode.
' So, aString must be converted intoa State object

    SetStateFromStateCode (aString)
End Property

Public Property Get ZipCode() As Long
    ZipCode = pvtZipCode
End Property

Public Property Let ZipCode(aLong As Long)
    pvtZipCode = aLong
'    ObjectHasChanged
End Property

Public Property Get ZipSupplement() As Integer
    ZipSupplement = pvtZipSupplement
End Property

Public Property Let ZipSupplement(anInteger As Integer)
    pvtZipSupplement = anInteger
'    ObjectHasChanged
End Property

Public Property Get ZipExtension() As Integer
    ZipExtension = pvtZipExtension
End Property

Public Function ZipExtensionString() As String
    If pvtZipExtension >= 0 Then
        ZipExtensionString = pvtZipExtension
    Else
        ZipExtensionString = ""
    End If
End Function

Public Function ZipSupplementString() As String
    If pvtZipSupplement >= 0 Then
        ZipSupplementString = pvtZipSupplement
    Else
        ZipSupplementString = ""
    End If
End Function


Public Property Let ZipExtension(anInteger As Integer)
    pvtZipExtension = anInteger
'    ObjectHasChanged
End Property

Public Property Get ForeignZipCode() As String
    ForeignZipCode = pvtForeignZipCode
End Property

Public Property Let ForeignZipCode(aString As String)
    pvtForeignZipCode = aString
'    ObjectHasChanged
End Property

Public Property Get Status() As String
    Status = pvtStatus
End Property

Public Property Let Status(aString As String)
    pvtStatus = aString
'    ObjectHasChanged
End Property

Public Property Get Usage() As String
    Usage = pvtUsage
End Property

Public Property Let Usage(aString As String)
    pvtUsage = aString
'    ObjectHasChanged
End Property

Public Function ObjectHasChanged()
' Mark this object as "Changed" and trigger the
'   "Changed" event

    On Local Error Resume Next

    ObjectChanged = True
    
#If NoEventMgr = False Then
    If Not ObjectManager Is Nothing Then
        ObjectManager. _
            TriggerObjectEvent _
                Event:="Changed", _
                Object:=Me
    End If
#End If
End Function



Public Function ObjectEventCallBack(Optional Event As Variant, Optional Object As Variant) As Long
' Receive the Trigger notification and process
'   accordingly
'
' Parameters:
'   Event
'       a string which identifies the Event
'       Example: "Changed", "Created", "Deleted"
'   Object
'       the object originating the Event.
'       responds to:
'           TypeName(TriggerObject)
'           TriggerObject.ObjectID
' (supported by VBOFEventManager)

End Function



